package com.mcglib.flac
{
	
	import com.mcglib.ds.DicMap;
	import com.mcglib.flac.buttonGroup.SimpleButtonRender;
	
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;

	
	[Event(name="valueChange",type="com.mcglib.flac.FlacEvent")]
	public class ButtonGroup extends FlacComponent
	{
		private var buttonDic:DicMap = new DicMap();
		
		private var _curValue:*;
		
		private var _curButton:InteractiveObject;
		
		//		private var _curButtonState:SimpleButton = new SimpleButton;
		
		//private var _selectBringInFront:Boolean;
		
		private var curButtonDepth:int;
		
		private var selectEventType:String = MouseEvent.CLICK;
		
		public function ButtonGroup()
		{
			super();
		}
		
		
		
		
		/*public function get selectBringInFront():Boolean
		{
		return _selectBringInFront;
		}
		
		
		public function set selectBringInFront(value:Boolean):void
		{
		if(_selectBringInFront==value)
		return;
		_selectBringInFront = value;
		sortButtonDepth();
		}*/
		
		/**
		 * 注册一个按钮 
		 * @param button 按钮实例
		 * @param value 对应的数据 如果该参数为空 那么对应的数据则为button本身
		 * @param render 控制器 如果render参数为空,则框架会自动new一个SimpleButtonRender代替
		 * 
		 */
		public function registerButton(button:InteractiveObject,value:*=null,render:IDataRenderer=null):void{
			if(!buttonDic.containKey(button)){
				
				if(render==null)
					render = new SimpleButtonRender;
				
				if(value==null)
					value = button;
				
				button.addEventListener(selectEventType,onButtonClick);
				button.addEventListener(MouseEvent.MOUSE_OVER,onBtnOver);
				button.addEventListener(MouseEvent.MOUSE_OUT,onBtnOut);
				
				render.initDataRenderer(button,this);
				
				
				render.data = value;
				
				buttonDic.put(button,render);		
			}else
				throw new Error(button+"已经被注册");
		}
		
		/**
		 * 注销一个按钮 
		 * @param button
		 * 
		 */
		public function removeButton(button:InteractiveObject):void{
			if(buttonDic.containKey(button)){
				button.removeEventListener(selectEventType,onButtonClick);
				button.removeEventListener(MouseEvent.MOUSE_OVER,onBtnOver);
				button.removeEventListener(MouseEvent.MOUSE_OUT,onBtnOut);
				
				
				buttonDic.deleteItem(button);
				
				if(button==curButton){
					_curButton = null;
					dispatchValueChangeEvent();
				}
			}	
		}
		
		public function setBtnSelectable(btn:InteractiveObject,s:Boolean):void{
			if(!buttonDic.containKey(btn))
				throw new Error(btn+"尚未注册到该ButtonGroup中");
			
			if(!s){
				if(curButton==btn)
					curButton = null;
			}
			
			getBtnProxy(btn).selectable = s;
		}
		
		public function getBtnSelectable(btn:InteractiveObject):Boolean{
			if(!buttonDic.containKey(btn))
				throw new Error(btn+"尚未注册到该ButtonGroup中");
			
			return getBtnProxy(btn).selectable;
		}
		
		public function get curValue():*{
			return _curButton?getBtnValue(_curButton):null;
		}
		
		public function get curButton():InteractiveObject
		{
			return _curButton;
		}
		
		public function set curButton(value:InteractiveObject):void
		{
			if(value!=null && !buttonDic.containKey(value))
				throw new Error(value+"尚未注册到该ButtonGroup中");
			
			if(value == _curButton)
				return;
			
			var btnProxy:IDataRenderer;
			if(value){
				btnProxy = getBtnProxy(value);
				if(!btnProxy.selectable)
					throw new Error(value+"已经被禁用");
			}
			
			if(_curButton){
				getBtnProxy(_curButton).selected = false;
			}
			
			
			_curButton = value;
			
			if(btnProxy){
				btnProxy.selected = true;
			}
			
			dispatchValueChangeEvent();
		}
		
		private function onButtonClick(e:MouseEvent):void{
			var button:InteractiveObject = e.currentTarget as InteractiveObject;
			var buttonProxy:IDataRenderer = getBtnProxy(button);
			if(buttonProxy.selectable){
				curButton = button;
			}
			
		}
		
		private function onBtnOver(e:MouseEvent):void{
			var button:InteractiveObject = e.currentTarget as InteractiveObject;
			var buttonProxy:IDataRenderer = getBtnProxy(button);
			if(buttonProxy.selectable){
				buttonProxy.onMouseOver(e);
			}
		}
		
		private function onBtnOut(e:MouseEvent):void{
			var button:InteractiveObject = e.currentTarget as InteractiveObject;
			var buttonProxy:IDataRenderer = getBtnProxy(button);
			if(buttonProxy.selectable){
				buttonProxy.onMouseOut(e);
			}
		}
		
		private function dispatchValueChangeEvent():void{
			var e:FlacEvent = new FlacEvent(FlacEvent.VALUE_CHANGE);
			e.render = getBtnProxy(_curButton);
			e.value = curValue;
			
			dispatchEvent(e);
		}
		
		public function getBtnValue(btn:InteractiveObject):*{
			return getBtnProxy(btn).data;
		}
		
		private function getBtnProxy(btn:InteractiveObject):IDataRenderer{
			return buttonDic.getValue(btn) as IDataRenderer;
		}
		
	}
}